package com.shanghaionstar.nav.share;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.widget.Toast;

import com.onstar.cn.common.util.CommonUtil;
import com.shanghaionstar.R;
import com.shanghaionstar.share.AbstractShareDialog;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ShareWXUtil;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;


/**
 * Created by Longmin Li on 2017/6/19.
 * 图片分享对话框
 */
public class ShareImgDialog extends AbstractShareDialog {
    private Bitmap shareBitmap;

    public Bitmap getShareBitmap() {
        return shareBitmap;
    }

    public void setShareBitmap(Bitmap shareBitmap) {
        this.shareBitmap = shareBitmap;
    }

    public ShareImgDialog(Context context) {
        super(context);
    }

    public ShareImgDialog(Context context, int theme) {
        super(context, theme);
    }

    public ShareImgDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    //分享到微信好友
    @Override
    public void shareToWX() {
        if (!CommonUtil.checkNetWorkState(context)) {
            Toast.makeText(context, context.getResources().getString(R.string.networkError),
                    Toast.LENGTH_LONG).show();
        }
        if (!weiXinApi.isWXAppInstalled()) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse("http://weixin.qq.com");
            intent.setData(content_url);
            context.startActivity(intent);
        } else {
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    new ShareWXUtil().shareBitmapImg(weiXinApi,
                            SendMessageToWX.Req.WXSceneSession, shareBitmap);
                }
            }.start();
        }
    }

    //微信朋友圈
    @Override
    public void shareToWXF() {
        if (!CommonUtil.checkNetWorkState(context)) {
            Toast.makeText(context, context.getString(R.string.networkError), Toast.LENGTH_LONG)
                    .show();
        }
        if (!weiXinApi.isWXAppInstalled()) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse("http://weixin.qq.com");
            intent.setData(content_url);
            context.startActivity(intent);
        } else {
            // 检测微信版本 4.2以上的 才支持分享到朋友圈 版本过低 提示用户下载
            if (weiXinApi.getWXAppSupportAPI() < 0x21020001) {
                AlertDialogUtil.showAlertDialog(context, "",
                        context.getString(R.string.weixin_version_too_old));
            } else {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        new ShareWXUtil().shareBitmapImg(weiXinApi,
                                SendMessageToWX.Req.WXSceneTimeline, shareBitmap);
                    }
                }.start();
            }
        }
    }

}
